package com.bookmanage.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bookmanage.dao.BookAddConn;

@WebServlet("/AddbookServlet")
public class AddbookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public AddbookServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置数据编码，防止插入到mysql乱码
        request.setCharacterEncoding("utf-8");
        //获取到要添加书籍的信息
        String bookname = request.getParameter("bookname");
        String author = request.getParameter("author");
        String publish = request.getParameter("publish");
        int isbn = Integer.parseInt(request.getParameter("isbn"));
        int price = Integer.parseInt(request.getParameter("price"));

        BookAddConn.add(isbn, price, author, publish, bookname);

        //解决中文乱码
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("<script>alert('书籍新增成功')</script>");
    }

}
